{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# VTK/Mayavi: Datasets Primer\n",
    "\n",
    "**Prabhu Ramachandran**\n",
    "\n",
    "**Department of Aerospace Engineering, IIT Bombay**\n",
    "\n",
    "<br/>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Outline\n",
    "\n",
    "- Introduction\n",
    "- VTK datasets in brief\n",
    "- Creating datasets from Python\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Outline\n",
    "\n",
    "- **Introduction**  $\\Longleftarrow$\n",
    "- VTK datasets\n",
    "- Creating datasets from Python\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Datasets: Why the fuss?\n",
    "\n",
    "* 3D data requires more information than in 2D\n",
    "\n",
    "* Topology is important\n",
    "\n",
    "* 2D is a lot easier\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Topology\n",
    "### Topology: mathematics\n",
    "\n",
    "> A branch of mathematics which studies the properties of geometrical\n",
    "> forms which retain their identity under certain transformations, such as\n",
    "> stretching or twisting, which are homeomorphic.\n",
    "\n",
    "-- from the Collaborative International Dictionary of English\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Topology\n",
    "### Topology: networking\n",
    "\n",
    "> Which hosts are directly connected to which other hosts in a network.\n",
    "> Network layer processes need to consider the current network topology to\n",
    "> be able to route packets to their final destination reliably and\n",
    "> efficiently.\n",
    "\n",
    "-- from the free On-line Dictionary of Computing\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Topology and Grids\n",
    "\n",
    "* Layman definition: how are points of the space connected up to\n",
    "  form a line/surface/volume\n",
    "\n",
    "* *Grid* in scientific computing: points + topology\n",
    "\n",
    "* Space broken into small \"pieces\" called\n",
    "    * Cells\n",
    "    * Elements\n",
    "\n",
    "* Data can be associated with the points or cells\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## The general idea\n",
    "\n",
    "* Specify points of the space\n",
    "\n",
    "* Specify connectivity between points (topology)\n",
    "\n",
    "* Connectivity creates \"cells\"\n",
    "\n",
    "* Specify \"attribute\" data at points or cells\n",
    "\n",
    "<img src=\"MEDIA/m2/datasets/dataset_idea.png\" width=\"60%\" height=\"25%\" />\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Types of datasets\n",
    "\n",
    "* Implicit topology (structured):\n",
    "   * Image data (structured points)\n",
    "   * Rectilinear grids\n",
    "   * Structured grids\n",
    "\n",
    "* Explicit topology (unstructured):\n",
    "   * Polygonal data (surfaces)\n",
    "   * Unstructured grids\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Structured versus unstructured grids\n",
    "\n",
    "* Important to understand the differences\n",
    "\n",
    "* Differences related to topology specification\n",
    "\n",
    "* Recall the `mlab` data sources?\n",
    "   * Unconnected\n",
    "   * Implicit connectivity\n",
    "   * Explicit connectivity\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Unconnected sources\n",
    "\n",
    "| `scalar_scatter` | `vector_scatter` |\n",
    "| ---------------- | ---------------- |\n",
    "| <img src=\"MEDIA/m2/mlab/points3d_ex.png\"/> | <img src=\"MEDIA/m2/mlab/quiver3d_ex.png\"/> |\n",
    "|`PolyData`      | `PolyData`    |\n",
    "|`mlab.points3d`   | `mlab.quiver3d` |\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Implicitly-connected sources\n",
    "\n",
    "| `scalar_field`    |   `vector_field`  |\n",
    "| ----------------- | ---------------   |\n",
    "| <img src=\"MEDIA/m2/mlab/contour3d.png\" height=\"75%\" width=\"100%\"/> | <img src=\"MEDIA/m2/mlab/flow_ex.png\" width=\"75%\" height=\"60%\" /> |\n",
    "| `ImageData`       | `ImageData`     |\n",
    "| `mlab.contour3d`  | `mlab.flow`     |\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Implicitly-connected sources\n",
    "\n",
    "`array2d_source`\n",
    "\n",
    "<img src=\"MEDIA/m2/mlab/imshow_ex.png\" width=\"50%\" height=\"50%\"/>\n",
    "\n",
    "`ImageData`\n",
    "\n",
    "`mlab.imshow`\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Explicitly-connected sources\n",
    "\n",
    "| `line_source`  | `triangular_mesh_source` |\n",
    "| -------------- | ---------------------- |\n",
    "| <img src=\"MEDIA/m2/mlab/plot3d_ex.png\"/> |  <img src=\"MEDIA/m2/mlab/triangular_mesh_ex.png\"/> |\n",
    "|`PolyData`      | `PolyData`    |\n",
    "|`mlab.plot3d`   | `mlab.triangular_mesh` |\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Structured grids\n",
    "\n",
    "* Implicit topology associated with points\n",
    "* Easiest example: a rectangular mesh\n",
    "* Non-rectangular mesh certainly possible\n",
    "\n",
    "\n",
    "|<img src=\"MEDIA/m2/datasets/rectangularsg.png\"/> | <img src=\"MEDIA/m2/datasets/sgrid.png\"/>|\n",
    "| ------ | ------- |\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Unstructured grids\n",
    "\n",
    "* Explicit topology specification\n",
    "\n",
    "* Specified via connectivity lists\n",
    "\n",
    "* Different number of neighbors, different types of cells\n",
    "\n",
    "<img src=\"MEDIA/m2/datasets/unstructured.png\" width=\"60%\" height=\"25%\"/>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Different types of cells\n",
    "\n",
    "<img src=\"MEDIA/m2/datasets/cells.png\"/>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Scalar, vector and tensor fields\n",
    "\n",
    "* Associate a scalar/vector/tensor with every point of the space\n",
    "\n",
    "* Scalar field: $ f(\\mathcal{R}^n) \\rightarrow \\mathcal{R}$\n",
    "* Vector field: $ f(\\mathcal{R}^n) \\rightarrow \\mathcal{R}^m$\n",
    "* Some examples:\n",
    "    * Temperature distribution on a rod\n",
    "    * Pressure distribution in room\n",
    "    * Velocity field in room\n",
    "    * Vorticity field in room\n",
    "    * Stress tensor field on a surface\n",
    "\n",
    "* Two aspects of field data, representation and visualization\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## A note on Cell Data\n",
    "\n",
    "* Most algorithms work with point data\n",
    "\n",
    "* Convert to point data: `CellDataToPointData`\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Exercise\n",
    "<center>\n",
    "<img src=\"MEDIA/m2/mlab/points3d_ex.png\"/>\n",
    "</center>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Exercise\n",
    "<center>\n",
    "<img src=\"MEDIA/m2/mlab/plot3d_ex.png\"/>\n",
    "</center>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Exercise\n",
    "<center>\n",
    "<img src=\"MEDIA/m2/mlab/surf_ex.png\"/>\n",
    "</center>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Exercise\n",
    "<center>\n",
    "<img src=\"MEDIA/m2/mlab/contour3d.png\"/>\n",
    "</center>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Exercise\n",
    "<center>\n",
    "<img src=\"MEDIA/m2/mlab/triangular_mesh.png\"/>\n",
    "</center>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Outline\n",
    "\n",
    "- Introduction\n",
    "- **VTK datasets** $\\Longleftarrow$\n",
    "- Creating datasets from Python\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Legacy VTK Data files\n",
    "\n",
    "* Detailed documentation on this is available here:\n",
    "    [www.vtk.org/VTK/img/file-formats.pdf](https://www.vtk.org/VTK/img/file-formats.pdf)\n",
    "\n",
    "* VTK data files support the following:\n",
    "  1. Structured points (Image data)\n",
    "  1. Rectilinear grid\n",
    "  1. Structured grid\n",
    "  1. Unstructured grid\n",
    "  1. Polygonal data\n",
    "\n",
    "* Binary and ASCII files are supported\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## The different datasets\n",
    "\n",
    "- Implicitly connected datasets\n",
    "\n",
    "   - Structured points/Image data: fixed spacing, orthogonal\n",
    "   - Rectilinear grid: spacing variable but orthogonal coordinates\n",
    "   - Structured grids: mappable to a meshgrid\n",
    "\n",
    "- Explicitly connected\n",
    "\n",
    "   - Polygonal data: surfaces\n",
    "   - Unstructured grid: volumes/surfaces\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Implicit ordering\n",
    "\n",
    "* Important: Implicit ordering of points and cells.\n",
    "  The $X$ co-ordinate increases first, $Y$ next and $Z$ last.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## General structure\n",
    "\n",
    "```\n",
    "# vtk DataFile Version 2.0\n",
    "A long string describing the file (256 chars)\n",
    "ASCII | BINARY\n",
    "DATASET [type]\n",
    "...\n",
    "\n",
    "POINT_DATA n\n",
    "...\n",
    "\n",
    "CELL_DATA n\n",
    "...\n",
    "```\n",
    "\n",
    "* Point and cell data can be supplied together.\n",
    "* `n` is the number of points or cells.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Simple example\n",
    "\n",
    "```\n",
    "# vtk DataFile Version 2.0\n",
    "Structured points example.\n",
    "ASCII\n",
    "DATASET STRUCTURED_POINTS\n",
    "DIMENSIONS 2 2 1\n",
    "ORIGIN 0.0 0.0 0.0\n",
    "SPACING 1.0 1.0 1.0\n",
    "\n",
    "POINT_DATA 4\n",
    "SCALARS Temperature float\n",
    "LOOKUP_TABLE default\n",
    "100 200\n",
    "300 400\n",
    "\n",
    "VECTORS velocity float\n",
    "0.0 0.0 0.0\n",
    "1.0 0.0 0.0\n",
    "0.0 1.0 0.0\n",
    "1.0 1.0 0.0\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Notes\n",
    "\n",
    "* This is a legacy format\n",
    "* New format is in XML\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Loading data with `mlab`\n",
    "\n",
    "This will open all supported files:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%gui qt\n",
    "import numpy as np\n",
    "from mayavi import mlab"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mlab.pipeline.open('data/room_vis.wrl')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mlab.pipeline.open('data/fire_ug.vtu')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Outline\n",
    "\n",
    "- Introduction\n",
    "- VTK datasets\n",
    "- **Creating datasets from Python** $\\Longleftarrow$\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Overview\n",
    "\n",
    "* Create datasets with TVTK and NumPy\n",
    "\n",
    "* Simple examples\n",
    "\n",
    "* Very handy when working with NumPy\n",
    "\n",
    "* No need to create VTK data files!\n",
    "\n",
    "* `PolyData, StructuredPoints,` `StructuredGrid` , `UnstructuredGrid`\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Overview\n",
    "\n",
    "* Using `tvtk`  in the following\n",
    "\n",
    "* `tvtk`  uses VTK underneath\n",
    "\n",
    "* Much easier to use than raw VTK\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tvtk.api import tvtk"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Structured Points: 2D\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "from scipy import special\n",
    "# The scalar values.\n",
    "x, y = np.mgrid[-10:10:51j, -10:10:51j]\n",
    "r = np.sqrt(x**2 + y**2)\n",
    "z = 5.0*special.j0(r)  # Bessel function of order 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Can't specify explicit points, they are implicit.\n",
    "# The volume specified using origin, spacing and dims.\n",
    "from tvtk.api import tvtk\n",
    "spoints = tvtk.StructuredPoints(origin=(-10, -10, 0),\n",
    "                                spacing=(0.4, 0.4, 1),\n",
    "                                dimensions=(51, 51, 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "# Transpose array data due to VTK's implicit ordering.\n",
    "# ravel it so the number of components is 1.\n",
    "spoints.point_data.scalars = z.T.ravel()\n",
    "spoints.point_data.scalars.name = 'scalar'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Visualizing it\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mlab.clf()\n",
    "# Add the dataset to the pipeline\n",
    "src = mlab.pipeline.add_dataset(spoints)\n",
    "\n",
    "warp = mlab.pipeline.warp_scalar(src)\n",
    "surf = mlab.pipeline.surface(warp)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<center>\n",
    "<img src=\"MEDIA/m2/datasets/structured_points2d.png\" width=\"50%\" height=\"40%\"/>\n",
    "</center>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Structured Points: 3D\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x, y, z = np.mgrid[-5:5:128j,-5:5:128j,\n",
    "                   -5:5:128j]\n",
    "scalars = np.sin(x*y*z)/(x*y*z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "spoints = tvtk.StructuredPoints(\n",
    "    origin=(-5.,-5,-5),\n",
    "    spacing=(10./127,10./127,10./127),\n",
    "    dimensions=(128,128,128)\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# The copy makes the data contiguous and the transpose\n",
    "# makes it suitable for display via tvtk.\n",
    "s = scalars.transpose().copy()\n",
    "spoints.point_data.scalars = s.ravel()\n",
    "spoints.point_data.scalars.name = 'scalars'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Visualizing it\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mlab.clf()\n",
    "# Add the dataset to the pipeline\n",
    "src = mlab.pipeline.add_dataset(spoints)\n",
    "\n",
    "cut = mlab.pipeline.scalar_cut_plane(src)\n",
    "contour = mlab.pipeline.iso_surface(src)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<center>\n",
    "<img src=\"MEDIA/m2/datasets/structured_points3d.png\" width=\"50%\" height=\"50%\"/>\n",
    "</center>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Structured Grid\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "r, th, z = np.mgrid[1:10:25j, 0:2*np.pi:51j, 0:5:25j]\n",
    "x, y = np.cos(th)*r, np.sin(th)*r\n",
    "scalar = x*x + y*y + z*z"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pts = np.empty(z.shape + (3,))\n",
    "pts[...,0] = x\n",
    "pts[...,1] = y\n",
    "pts[...,2] = z"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pts = pts.transpose(2, 1, 0, 3).copy()\n",
    "pts.shape = pts.size//3, 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "sgrid = tvtk.StructuredGrid(dimensions=x.shape)\n",
    "sgrid.points = pts\n",
    "sgrid.point_data.scalars = np.ravel(scalar.T.copy())\n",
    "sgrid.point_data.scalars.name = 'scalars'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Visualizing it\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mlab.clf()\n",
    "# Add the dataset to the pipeline\n",
    "src = mlab.pipeline.add_dataset(sgrid)\n",
    "\n",
    "plane = mlab.pipeline.grid_plane(src)\n",
    "plane.grid_plane.axis = 'z'\n",
    "c_plane = mlab.pipeline.contour_grid_plane(src)\n",
    "c_plane.enable_contours = False\n",
    "\n",
    "iso = mlab.pipeline.iso_surface(src)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<center>\n",
    "<img src=\"MEDIA/m2/datasets/structured_grid.png\" width=\"50%\" height=\"50%\"/>\n",
    "</center>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## PolyData\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# The points in 3D.\n",
    "points = np.array([[0.,0,0], [1,0,0], [0,1,0], [0,0,1]])\n",
    "# Connectivity via indices to the points.\n",
    "triangles = np.array([[0,1,3], [0,3,2], [1,2,3], [0,2,1]])\n",
    "# Creating the data object."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "mesh = tvtk.PolyData()\n",
    "mesh.points = points # the points\n",
    "mesh.polys = triangles # triangles for connectivity.\n",
    "# For lines/verts: mesh.lines = lines; mesh.verts = verts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Now create some point data.\n",
    "temperature = np.array([10., 20. ,30., 40.], 'f')\n",
    "mesh.point_data.scalars = temperature\n",
    "mesh.point_data.scalars.name = 'temperature'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Some vectors.\n",
    "velocity = np.array([[0.,0.,0], [1.,0,0], [0.,1,0], [0.,0,1]])\n",
    "mesh.point_data.vectors = velocity\n",
    "mesh.point_data.vectors.name = 'velocity'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Visualizing it\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mlab.clf()\n",
    "src = mlab.pipeline.add_dataset(mesh)\n",
    "\n",
    "surf = mlab.pipeline.surface(src)\n",
    "vec = mlab.pipeline.vectors(src)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<center>\n",
    "<img src=\"MEDIA/m2/datasets/polydata.png\" width=\"50%\" height=\"50%\"/>\n",
    "</center>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Unstructured Grid\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "points = np.array([[0.,0.,0], [1.,0,0], [0.,1,0], [0.,0,1]])\n",
    "tets = np.array([[0, 1, 2, 3]])\n",
    "tet_type = tvtk.Tetra().cell_type # VTK_TETRA == 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ug = tvtk.UnstructuredGrid(points=points)\n",
    "# This sets up the cells.\n",
    "ug.set_cells(tet_type, tets)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Attribute data.\n",
    "temperature = np.array([10, 20 ,20, 30], 'f')\n",
    "ug.point_data.scalars = temperature\n",
    "ug.point_data.scalars.name = 'temperature'\n",
    "# Some vectors.\n",
    "velocity = np.array([[0.,0,0], [1,0,0], [0,1,0], [0,0,1]])\n",
    "ug.point_data.vectors = velocity\n",
    "ug.point_data.vectors.name = 'velocity'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Visualizing it\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mlab.clf()\n",
    "# Add the dataset to the pipeline\n",
    "src = mlab.pipeline.add_dataset(ug)\n",
    "\n",
    "surf = mlab.pipeline.surface(src)\n",
    "vec = mlab.pipeline.vectors(src)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<center>\n",
    "<img src=\"MEDIA/m2/datasets/ug.png\" width=\"50%\" height=\"50%\"/>\n",
    "</center>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Saving data to file\n",
    "\n",
    "* Use `tvtk.api.write_data`\n",
    "* Automatically picks a writer\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tvtk.api import write_data\n",
    "write_data(ug, '/tmp/ug.vtu')\n",
    "write_data(ug, '/tmp/ug.vtk')"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  },
  "livereveal": {
   "controls": true,
   "help": true,
   "scroll": true,
   "slideNumber": true,
   "transition": "none"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
